package com.ukao.steward.ui.valetRunners.toShop;

import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ukao.steward.R;
import com.ukao.steward.adapter.ShopClothingFragmentPagerAdapter;
import com.ukao.steward.base.BaseFragment;
import com.ukao.steward.widget.NoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link ToShopCheckFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link ToShopCheckFragment#newInstance} factory method to
 * create an instance of this fragment.
 *    到店核查
 */
public class ToShopCheckFragment extends BaseFragment  {

    private Unbinder unbinder;

    private  static  final String CODE_ID ="id";        //单号ID
    private  static final  String ODD_NUMBERS ="batchCode";
    private  static final  String DATA_LOAD_MODE ="dataLoadMode";  //数据加载类别



    private int  id;
    private String numbers;
    private int dataLoadMode;
    private OnFragmentInteractionListener mListener;
    @BindView(R.id.back_btn)
    ImageView ivBackBtn;
    @BindView(R.id.title)
    TextView tvTitle;
    @BindView(R.id.to_shop_tab_layout)
    TabLayout tlTopNavigationBar;
    @BindView(R.id.view_pager)
    NoScrollViewPager vpViewPager;
    TextView tvTopOne, tvTopTwo, tvTopThree;

    public ToShopCheckFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment ToShopCheckFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static ToShopCheckFragment newInstance(int id, String numbers, int dataLoadMode ) {
        ToShopCheckFragment fragment = new ToShopCheckFragment();
        Bundle args = new Bundle();
        args.putInt(CODE_ID, id);
        args.putString(ODD_NUMBERS, numbers);
        args.putInt(DATA_LOAD_MODE,dataLoadMode);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            id = getArguments().getInt(CODE_ID);
            numbers = getArguments().getString(ODD_NUMBERS);
            dataLoadMode=getArguments().getInt(DATA_LOAD_MODE);
        }
    }



    @Override
    protected void initView() {
         switch (dataLoadMode){
             case ToShopFragment.BATCH_TO_SHOP_FLAG:
                 tvTitle.setText("批号:"+ numbers);  //设置批号
                 break;
             case ToShopFragment.ORDER_TO_SHOP_FLAG:
                 tvTitle.setText("单号:"+ numbers);  //设置单号
                 break;
         }

        //准备数据源
        List<Fragment>   fragmentList=new ArrayList<>();
        fragmentList.add(GoodsToShopDetailsFragment.newInstance(1,String.valueOf(id),dataLoadMode));
        fragmentList.add(GoodsToShopDetailsFragment.newInstance(2,String.valueOf(id),dataLoadMode));
        fragmentList.add(GoodsToShopDetailsFragment.newInstance(3,String.valueOf(id),dataLoadMode));
        //设置适配器
        ShopClothingFragmentPagerAdapter fragmentPagerAdapter=new
                ShopClothingFragmentPagerAdapter(getChildFragmentManager(),fragmentList);
        vpViewPager.setAdapter(fragmentPagerAdapter);
        vpViewPager.setOffscreenPageLimit(2);   //设置预加载
        //将TabLayout与ViewPager绑定在一起
        tlTopNavigationBar.setupWithViewPager(vpViewPager);

        String[] titles  =getResources().getStringArray(R.array.to_shop_tab_titles);
        //设置自定义tab1
        TabLayout.Tab tabOne = tlTopNavigationBar.getTabAt(0);
        tabOne.setCustomView(R.layout.tab_layout_item);
        tvTopOne =tabOne.getCustomView().findViewById(R.id.tvOne);
//        tvTopOne.setText(String.valueOf(amount));
        tvTopOne.setTextColor(Color.parseColor("#10c011"));
        TextView tvBottom1= tlTopNavigationBar.getTabAt(0).getCustomView().findViewById(R.id.tvTwo);
        tvBottom1.setText(titles[0]);
        //选中
        //tab2
        TabLayout.Tab tabTwo = tlTopNavigationBar.getTabAt(1);
        tabTwo.setCustomView(R.layout.tab_layout_item);
        tvTopTwo =tabTwo.getCustomView().findViewById(R.id.tvOne);
//        tvTopTwo.setText(String.valueOf(toShopCount));
        tvTopTwo.setTextColor(Color.parseColor("#10c011"));
        TextView tvBottom2=tabTwo.getCustomView().findViewById(R.id.tvTwo);
        tvBottom2.setText(titles[1]);
        //tab3
        TabLayout.Tab tabThree = tlTopNavigationBar.getTabAt(2);
        tabThree.setCustomView(R.layout.tab_layout_item);
        tvTopThree =tabThree.getCustomView().findViewById(R.id.tvOne);
//        tvTopThree.setText(String.valueOf(notToSopCount));
        tvTopThree.setTextColor(Color.parseColor("#fd5859"));
        TextView tvBottom3=tabThree.getCustomView().findViewById(R.id.tvTwo);
        tvBottom3.setText(titles[2]);
        //添加点击事件
        tlTopNavigationBar.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //点击下一个tab之后，上一个tab退出被选中状态之前需要做的动作

            }
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });

    }

    /**
     * 改变tab布局
     */
    public void changeTabLayout(int total, int toShopCount, int notToSopCount){
        tvTopOne.setText(String.valueOf(total));
        tvTopTwo.setText(String.valueOf(toShopCount));
        tvTopThree.setText(String.valueOf(notToSopCount));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =inflater.inflate(R.layout.fragment_to_shop_check, container, false);
        unbinder = ButterKnife.bind(this,view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //初始化view
        initView();
    }
    @OnClick(R.id.back_btn)
   public  void  onViewClicked(View view){
         switch (view.getId()){
             case R.id.back_btn:
                 finish();
                 break;
         }
   }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
//        if (context instanceof OnFragmentInteractionListener) {
//            mListener = (OnFragmentInteractionListener) context;
//        } else {
//            throw new RuntimeException(context.toString()
//                    + " must implement OnFragmentInteractionListener");
//        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();  //接触绑定
    }



    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
